export function formatDateTime2WeekDate(dateTimeString) {
    const date = new Date(dateTimeString);

    const year = date.getFullYear();
    const month = String(date.getMonth() + 1).padStart(2, '0');
    const day = String(date.getDate()).padStart(2, '0');

    const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
    const weekday = weekdays[date.getDay()];

    const hour = String(date.getHours()).padStart(2, '0');
    const minute = String(date.getMinutes()).padStart(2, '0');

    return `${year}年${month}月${day}日 ${weekday} ${hour}:${minute}`;
}

export function getOverTime(t1) {
    const time1 = new Date(t1);
    const time2 = new Date();
    // 将 t1 增加 15 分钟 额外增加1s
    const t1Plus15Minutes = new Date(time1.getTime() + 15 * 60 * 1000).getTime() + 1000;
    const t2 = time2.getTime();
    // 计算差值（以毫秒为单位）
    const difference = t1Plus15Minutes - t2;

    // 如果差值小于等于0，表示 t2 大于 t1 + 15分钟
    if (difference <= 0) {
        return [0, 0];
    }

    // 计算差值对应的分钟和秒数
    const minutes = Math.floor(difference / (1000 * 60));
    const seconds = Math.floor((difference % (1000 * 60)) / 1000);

    return [minutes, seconds];

}

export function format2StandardDateStr(parsedDate) {
    return parsedDate.getFullYear() + '-' + ('0' + (parsedDate.getMonth() + 1)).slice(-2) + '-' + ('0' + parsedDate.getDate()).slice(-2) + ' 00:00:00';
}

export function format2CommentStandardDateStr(dateStr) {
    let parsedDate = new Date(dateStr);
    return parsedDate.getFullYear() + '-' + ('0' + (parsedDate.getMonth() + 1)).slice(-2) + '-' + ('0' + parsedDate.getDate()).slice(-2) +
        (' ') + ('0' + (parsedDate.getHours())).slice(-2) + ':' + ('0' + (parsedDate.getMinutes())).slice(-2);
}


export function getSaleStartTime(saleTicketStartTime) {
    // 获取当前日期和时间
    let today = new Date();
    today.setHours(0, 0, 0, 0); // 将时间部分设置为零时零分零秒

    // 比较提供的日期和当前日期
    let saleTime = new Date(saleTicketStartTime);
    saleTime.setHours(0, 0, 0, 0); // 将时间部分设置为零时零分零秒

    // 格式化日期的函数
    function formatTime(date) {
        let hours = date.getHours().toString().padStart(2, '0');
        let minutes = date.getMinutes().toString().padStart(2, '0');
        return `${hours}:${minutes}`;
    }

    if (saleTime.getTime() < today.getTime()) return '已开售';
    if (saleTime.getTime() === today.getTime()) {
        return `今天 ${formatTime(saleTicketStartTime)} 开抢`;
    } else if (saleTime.getTime() === today.getTime() + 24 * 60 * 60 * 1000) {
        return `明天 ${formatTime(saleTicketStartTime)} 开抢`;
    } else if (saleTime.getTime() === today.getTime() + 24 * 60 * 60 * 1000 * 2) {
        return `后天 ${formatTime(saleTicketStartTime)} 开抢`;
    }
    return format2StandardDateStr(saleTime) + ' 开抢';
}

export function calculateTimeRemaining(targetDate) {
    var now = new Date();
    var difference = targetDate - now;
    var hours = Math.floor(difference / 1000 / 60 / 60);
    difference -= hours * 1000 * 60 * 60;
    var minutes = Math.floor(difference / 1000 / 60);
    difference -= minutes * 1000 * 60;
    var seconds = Math.floor(difference / 1000);

    return [hours, minutes, seconds];
}

export function formatNewsPerformanceTime(date) {
    return `${(date.getMonth() + 1).toString().padStart(2, '0')}.${date.getDate().toString().padStart(2, '0')} ${date.getHours().toString().padStart(2, '0')}:${date.getMinutes().toString().padStart(2, '0')}`;
}